package geyue.wechat.util;

import java.io.Console;

public class bytesUtil {
	public static void main(String[] args) {
//		int a[] = {1};
//		int b[] = {2};  
//		a = pj(a, b); 
//		
//		
//		
//		testTihuan();
//		testTihuanByte();
		byte[] bts=bytesUtil.stringToBytes("hello");
		System.out.println(bts.length);
		System.out.println(new String(bts));
	}

	private static byte[] stringToBytes(String str) {
		return 	str.getBytes(); 
	}
	
	private static void testTihuanByte() {
		//正常
		showResult1_byte(); 
		//位置过大 
		showResult2_byte();
		//位置过小
		showResult3_byte();
		//原来的数据为空时
		showResult4_byte(); 
		//要添加的数据为空时
		showResult5_byte(); 
		//添加的数据和元来的数据都为空时
		showResult6_byte();  
		showResult7_byte(); 
		showResult8_byte();  
		
	}


	private static void showResult8_byte() {
		// TODO Auto-generated method stub
		byte na[] = {1,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,3}; 
		byte aa[] = {1,2,3,1,2,3,1,2,3,0,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3}; 
		int spos =23;  
		showResultByte(na, aa, spos); 
		
	}


	private static void showResult7_byte() {
		byte na[] = {1,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,3}; 
		byte aa[] = {1,2,3,1,2,3,1,2,3,0,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31}; 
		int spos =33;  
		showResultByte(na, aa, spos); 
		
	}


	private static void showResult6_byte() {
		// TODO Auto-generated method stub
		byte na[] = null; 
		byte aa[] = null; 
		int spos =1;  
		showResultByte(na, aa, spos); 
		
	}


	private static void showResult5_byte() {
		// TODO Auto-generated method stub
		byte na[] = {33}; 
		byte aa[] = null; 
		int spos =1;  
		showResultByte(na, aa, spos); 
		
	}


	private static void showResult4_byte() {
		// TODO Auto-generated method stub
		byte na[] = null; 
		byte aa[] = {1,2,3}; 
		int spos =2; 
		showResultByte(na, aa, spos);
		
	}


	private static void showResult3_byte() {
		// TODO Auto-generated method stub
		byte na[] = {33}; 
		byte aa[] = {1,2,3}; 
		int spos =-1000; 
		showResultByte(na, aa, spos);
		
	}


	private static void showResult2_byte() {
		// TODO Auto-generated method stub
		byte na[] = {33}; 
		byte aa[] = {1,2,3}; 
		int spos =1000; 
		showResultByte(na, aa, spos);
		
	}


	private static void showResult1_byte() {
		// TODO Auto-generated method stub
		byte na[] = "我".getBytes(); 
		byte aa[] = "这是原始数据".getBytes(); 
		int spos =1;  
		showResultByte(na, aa, spos);
		
	}


	private static void testTihuan() { 
		//正常
		showResult1(); 
		//位置过大 
		showResult2();
		//位置过小
		showResult3();
		//原来的数据为空时
		showResult4(); 
		//要添加的数据为空时
		showResult5(); 
		//添加的数据和元来的数据都为空时
		showResult6();  
		showResult7(); 
		showResult8();  
	}


	private static void showResult8() {
		int na[] = {1,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,3}; 
		int aa[] = {1,2,3,1,2,3,1,2,3,0,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3}; 
		int spos =23;  
		showResult(na, aa, spos); 
		
	}


	private static void showResult7() {
		int na[] = {1,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,3}; 
		int aa[] = {1,2,3,1,2,3,1,2,3,0,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31,2,31}; 
		int spos =33;  
		showResult(na, aa, spos); 
	}


	private static void showResult6() {
		int na[] = null; 
		int aa[] = null; 
		int spos =1;  
		showResult(na, aa, spos); 
	}


	private static void showResult5() {
		// TODO Auto-generated method stub
		int na[] = {33}; 
		int aa[] = null; 
		int spos =1;  
		showResult(na, aa, spos); 
	}


	private static void showResult4() {
		// TODO Auto-generated method stub
		int na[] = null; 
		int aa[] = {1,2,3}; 
		int spos =2; 
		showResult(na, aa, spos);
	}


	private static void showResult3() {
		// TODO Auto-generated method stub
		int na[] = {33}; 
		int aa[] = {1,2,3}; 
		int spos =-1000; 
		showResult(na, aa, spos);
	}


	private static void showResult2() {
		// TODO Auto-generated method stub
		int na[] = {33}; 
		int aa[] = {1,2,3}; 
		int spos =1000; 
		showResult(na, aa, spos);
		
	}


	private static void showResult1() {
		// TODO Auto-generated method stub
		int na[] = {33}; 
		int aa[] = {1,2,3}; 
		int spos =1;  
		showResult(na, aa, spos); 
		
	}


	private static void showResult(int[] na, int[] aa, int spos) {
		int ra[] = tihuan(na, aa, spos, false); 
		System.out.print("test：\t");
		if(ra!=null){
			for (int i : ra) {
				System.out.print(i+" ");
			}
			System.out.println();
		} else{
			System.out.println("数据为空.");
		} 
	}
	private static void showResultByte(byte[] na, byte[] aa, int spos) {
		byte ra[] = tihuan(na, aa, spos, false); 
		System.out.print("test：\t");
		if(ra!=null){
			for (int i : ra) {
				System.out.print(i+" ");
			}
			System.out.println();
		} else{
			System.out.println("数据为空.");
		} 
	}
	
	
	/**
	 * 
	 * @param newArray 要添加的数据
	 * @param allArray 原来的数据
	 * @param startPos 开始位置
	 * @param override 添加的数据，不足以填充从开始位置到原来数据末尾，是否舍弃后部分数据
	 * @return 添加后的总数据
	 */
	public static byte[] tihuan(byte[] newArray, byte[] allArray,int startPos,boolean override){
		  
		if(newArray==null||allArray==null){ 
			return null;
		}

		int nlen = newArray.length;
		int alen = allArray.length;
		int spos = startPos;
		
		if(spos>alen){//开始位置大于原来的数据长度 
			spos = alen; 
		}else if(spos<0){
			spos =0;
		} 
		int rlen = 0;
		byte resultArray[] =null;
		if(nlen+spos<=alen){
			//如果开始位置，到新数据长度，小于或等于总数据长度的话，
			//新的数组的长度为总数据长度

			if (override) {
				//覆盖则长度为新的数据长度
				rlen = spos+nlen;  
				resultArray  = new byte[rlen]; 
				System.arraycopy(allArray,0,resultArray,0,spos );//之前
				System.arraycopy(newArray, 0, resultArray, spos, nlen);//新数据 

			}else{
				rlen = alen;//不覆盖则为原有长度
				resultArray  = new byte[rlen]; 
				System.arraycopy(allArray,0,resultArray,0,spos );//之前
				System.arraycopy(newArray, 0, resultArray, spos, nlen); //新数据
				System.arraycopy(allArray, spos+nlen, resultArray, spos+nlen, alen-spos-nlen);//之后

			} 
		}else{
			//
			//如果开始位置加上新数据长度，大于总数据长度
			//新数据数组的长度应该为， 大出来的那个数据的长度+原来总的数据长度。
			rlen = spos+nlen; 
			resultArray = new byte[rlen]; 
	
		} 
		return resultArray;

	}


	/**
	 * 
	 * @param newArray 要添加的数据
	 * @param allArray 原来的数据
	 * @param startPos 开始位置
	 * @param override 添加的数据，不足以填充从开始位置到原来数据末尾，是否舍弃后部分数据
	 * @return 添加后的总数据
	 */
	public static int [] tihuan(int[] newArray, int[] allArray,int startPos,boolean override){
		  
		if(newArray==null||allArray==null){ 
			return null;
		}

		int nlen = newArray.length;
		int alen = allArray.length;
		int spos = startPos;
		
		if(spos>alen){//开始位置大于原来的数据长度 
			spos = alen; 
		}else if(spos<0){
			spos =0;
		} 
		int rlen = 0;
		int resultArray[] =null;
		if(nlen+spos<=alen){
			//如果开始位置，到新数据长度，小于或等于总数据长度的话，
			//新的数组的长度为总数据长度

			if (override) {
				//覆盖则长度为新的数据长度
				rlen = spos+nlen;  
				resultArray  = new int[rlen]; 
				System.arraycopy(allArray,0,resultArray,0,spos );//之前
				System.arraycopy(newArray, 0, resultArray, spos, nlen);//新数据 

			}else{
				rlen = alen;//不覆盖则为原有长度
				resultArray  = new int[rlen]; 
				System.arraycopy(allArray,0,resultArray,0,spos );//之前
				System.arraycopy(newArray, 0, resultArray, spos, nlen); //新数据
				System.arraycopy(allArray, spos+nlen, resultArray, spos+nlen, alen-spos-nlen);//之后

			} 
		}else{
			//
			//如果开始位置加上新数据长度，大于总数据长度
			//新数据数组的长度应该为， 大出来的那个数据的长度+原来总的数据长度。
			rlen = spos+nlen; 
			resultArray = new int[rlen]; 
			System.arraycopy(allArray,0,resultArray,0,spos );//之前的数据
			System.arraycopy(newArray, 0, resultArray, spos, nlen); // 新数据
			
		} 
		return resultArray;

	}

	private static int[] pj(int[] a, int[] b) {
		int[] ab = new int[a.length+b.length]; 
		System.arraycopy(a, 0, ab, 0, a.length); 
		for (int i : ab) {
			System.out.print(i+" "); 
		}
		System.out.println();
		return ab;
	}
}
